<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Application Quick Start - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.application.quick-start.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.application.quick-start.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.application.introduction.html">Introdu&ccedil;&atilde;o</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.application.html">Zend_Application</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.theory-of-operation.html">Theory of Operation</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.application.quick-start" class="section"><div class="info"><h1 class="title">Zend_Application Quick Start</h1></div>
    

    <p class="para">
        There are two paths to getting started with
        <span class="classname">Zend_Application</span>, and they depend on how you start your
        project. In each case, you always start with creating a
        <span class="classname">Bootstrap</span> class, and a related configuration file.
    </p>

    <p class="para">
        If you plan on using <span class="classname">Zend_Tool</span> to create your project,
        continue reading below. If you will be adding
        <span class="classname">Zend_Application</span> to an existing project, you&#039;ll want to
        <a href="zend.application.quick-start.html#zend.application.quick-start.manual" class="link">skip ahead</a>.
    </p>

    <div class="section" id="zend.application.quick-start.zend-tool"><div class="info"><h1 class="title">Using Zend_Tool</h1></div>
        

        <p class="para">
            The quickest way to start using <span class="classname">Zend_Application</span> is to use
            <span class="classname">Zend_Tool</span> to generate your project. This will also create
            your <span class="classname">Bootstrap</span> class and file.
        </p>

        <p class="para">
            To create a project, execute the <strong class="command">zf</strong> command (on *nix systems):
        </p>

        <pre class="programlisting brush: sh">
% zf create project newproject
</pre>


        <p class="para">
            Or the Windows <var class="filename">zf.bat</var> command:
        </p>

        <pre class="programlisting brush: dos">
C:&gt; zf.bat create project newproject
</pre>


        <p class="para">
            Both will create a project structure that looks like the following:
        </p>

        <pre class="programlisting brush: text">
newproject
|-- application
|   |-- Bootstrap.php
|   |-- configs
|   |   `-- application.ini
|   |-- controllers
|   |   |-- ErrorController.php
|   |   `-- IndexController.php
|   |-- models
|   `-- views
|       |-- helpers
|       `-- scripts
|           |-- error
|           |   `-- error.phtml
|           `-- index
|               `-- index.phtml
|-- library
|-- public
|   `-- index.php
`-- tests
    |-- application
    |   `-- bootstrap.php
    |-- library
    |   `-- bootstrap.php
    `-- phpunit.xml
</pre>


        <p class="para">
            In the above diagram, your bootstrap is in
            <var class="filename">newproject/application/Bootstrap.php</var>, and looks like
            the following at first:
        </p>

        <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
</pre>


        <p class="para">
            You&#039;ll also note that a configuration file,
            <var class="filename">newproject/application/configs/application.ini</var>, is
            created. It has the following contents:
        </p>

        <pre class="programlisting brush: dosini">
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH &quot;/../library&quot;
bootstrap.path = APPLICATION_PATH &quot;/Bootstrap.php&quot;
bootstrap.class = &quot;Bootstrap&quot;
resources.frontController.controllerDirectory = APPLICATION_PATH &quot;/controllers&quot;

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
</pre>


        <p class="para">
            All settings in this configuration file are for use with
            <span class="classname">Zend_Application</span> and your bootstrap.
        </p>

        <p class="para">
            Another file of interest is the
            <var class="filename">newproject/public/index.php</var> file, which invokes
            <span class="classname">Zend_Application</span> and dispatches it.
        </p>

        <pre class="programlisting brush: php">
// Define path to application directory
defined(&#039;APPLICATION_PATH&#039;)
    || define(&#039;APPLICATION_PATH&#039;,
              realpath(dirname(__FILE__) . &#039;/../application&#039;));

// Define application environment
defined(&#039;APPLICATION_ENV&#039;)
    || define(&#039;APPLICATION_ENV&#039;,
              (getenv(&#039;APPLICATION_ENV&#039;) ? getenv(&#039;APPLICATION_ENV&#039;)
                                         : &#039;production&#039;));

/** Zend_Application */
require_once &#039;Zend/Application.php&#039;;

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . &#039;/configs/application.ini&#039;
);
$application-&gt;bootstrap()
            -&gt;run();
</pre>


        <p class="para">
            To continue the quick start, please <a href="zend.application.quick-start.html#zend.application.quick-start.resources" class="link">skip to the
            Resources section</a>.
        </p>
    </div>

    <div class="section" id="zend.application.quick-start.manual"><div class="info"><h1 class="title">Adding Zend_Application to your application</h1></div>
        

        <p class="para">
            The basics of <span class="classname">Zend_Application</span> are fairly simple:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    Create an <var class="filename">application/Bootstrap.php</var> file, with the
                    class <span class="classname">Bootstrap</span>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    Create an <var class="filename">application/configs/application.ini</var>
                    configuration file with the base configuration necessary for
                    <span class="classname">Zend_Application</span>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    Modify your <var class="filename">public/index.php</var> to utilize
                    <span class="classname">Zend_Application</span>.
                </p>
            </li>
        </ul>

        <p class="para">
            First, create your <span class="classname">Bootstrap</span> class. Create a file,
            <var class="filename">application/Bootstrap.php</var>, with the following contents:
        </p>

        <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
</pre>


        <p class="para">
            Now, create your configuration. For this tutorial, we will use an
            <acronym class="acronym">INI</acronym> style configuration; you may, of course, use an
            <acronym class="acronym">XML</acronym>, <acronym class="acronym">JSON</acronym>, <acronym class="acronym">YAML</acronym>, or
            <acronym class="acronym">PHP</acronym> configuration file as well. Create the file
            <var class="filename">application/configs/application.ini</var>, and provide the following
            contents:
        </p>

        <pre class="programlisting brush: dosini">
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH &quot;/../library&quot;
bootstrap.path = APPLICATION_PATH &quot;/Bootstrap.php&quot;
bootstrap.class = &quot;Bootstrap&quot;
resources.frontController.controllerDirectory = APPLICATION_PATH &quot;/controllers&quot;

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
</pre>


        <p class="para">
            Now, let&#039;s modify your gateway script,
            <var class="filename">public/index.php</var>. If the file does not exist, create
            it; otherwise, replace it with the following contents:
        </p>

        <pre class="programlisting brush: php">
// Define path to application directory
defined(&#039;APPLICATION_PATH&#039;)
    || define(&#039;APPLICATION_PATH&#039;,
              realpath(dirname(__FILE__) . &#039;/../application&#039;));

// Define application environment
defined(&#039;APPLICATION_ENV&#039;)
    || define(&#039;APPLICATION_ENV&#039;,
              (getenv(&#039;APPLICATION_ENV&#039;) ? getenv(&#039;APPLICATION_ENV&#039;)
                                         : &#039;production&#039;));

// Typically, you will also want to add your library/ directory
// to the include_path, particularly if it contains your ZF installed
set_include_path(implode(PATH_SEPARATOR, array(
    dirname(dirname(__FILE__)) . &#039;/library&#039;,
    get_include_path(),
)));

/** Zend_Application */
require_once &#039;Zend/Application.php&#039;;

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . &#039;/configs/application.ini&#039;
);
$application-&gt;bootstrap()
            -&gt;run();
</pre>


        <p class="para">
            You may note that the application environment constant value looks
            for an environment variable &quot;APPLICATION_ENV&quot;. We recommend setting
            this in your web server environment. In Apache, you can set this
            either in your vhost definition, or in your <var class="filename">.htaccess</var>
            file. We recommend the following contents for your
            <var class="filename">public/.htaccess</var> file:
        </p>

        <pre class="programlisting brush: conf">
SetEnv APPLICATION_ENV development

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</pre>


        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Learn about mod_rewrite</b><br /></span>
            

            <p class="para">
                The above rewrite rules allow access to any file under your
                virtual host&#039;s document root. If there are files you do not want
                exposed in this way, you may want to be more restrictive in your
                rules. Go to the Apache website to <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html" class="link external">&raquo; learn
                more about mod_rewrite</a>.
            </p>
        </p></blockquote>

        <p class="para">
            At this point, you&#039;re all set to start taking advantage of
            <span class="classname">Zend_Application</span>.
        </p>
    </div>

    <div class="section" id="zend.application.quick-start.resources"><div class="info"><h1 class="title">Adding and creating resources</h1></div>
        

        <p class="para">
            If you followed the directions above, then your bootstrap class
            will be utilizing a front controller, and when it is run, it will
            dispatch the front controller. However, in all likelihood, you&#039;ll
            need a little more configuration than this.
        </p>

        <p class="para">
            In this section, we&#039;ll look at adding two resources to your
            application. First, we&#039;ll set up your layouts, and then we&#039;ll
            customize your view object.
        </p>

        <p class="para">
            One of the standard resources provided with
            <span class="classname">Zend_Application</span> is the &quot;layout&quot; resource. This
            resource expects you to define configuration values which it will
            then use to configure your <span class="classname">Zend_Layout</span> instance.
        </p>

        <p class="para">
            To use it, all we need to do is update the configuration file.
        </p>

        <pre class="programlisting brush: dosini">
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
bootstrap.path = APPLICATION_PATH &quot;/Bootstrap.php&quot;
bootstrap.class = &quot;Bootstrap&quot;
resources.frontController.controllerDirectory = APPLICATION_PATH &quot;/controllers&quot;
; ADD THE FOLLOWING LINES
resources.layout.layout = &quot;layout&quot;
resources.layout.layoutPath = APPLICATION_PATH &quot;/layouts/scripts&quot;

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
</pre>


        <p class="para">
            If you haven&#039;t already, create the directory
            <var class="filename">application/layouts/scripts/</var>, and the file
            <var class="filename">layout.phtml</var> within that directory. A good starting
            layout is as follows (and ties in with the view resource covered
            next):
        </p>

        <pre class="programlisting brush: php">
&lt;?php echo $this-&gt;doctype() ?&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;?php echo $this-&gt;headTitle() ?&gt;
    &lt;?php echo $this-&gt;headLink() ?&gt;
    &lt;?php echo $this-&gt;headStyle() ?&gt;
    &lt;?php echo $this-&gt;headScript() ?&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;?php echo $this-&gt;layout()-&gt;content ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>


        <p class="para">
            At this point, you will now have a working layout.
        </p>

        <p class="para">
            Now, we&#039;ll add a custom view resource. When initializing the view,
            we&#039;ll want to set the <acronym class="acronym">HTML</acronym> DocType and a default value for the title
            to use in the <acronym class="acronym">HTML</acronym> head. This can be accomplished by editing your
            <span class="classname">Bootstrap</span> class to add a method:
        </p>

        <pre class="programlisting brush: php">
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initView()
    {
        // Initialize view
        $view = new Zend_View();
        $view-&gt;doctype(&#039;XHTML1_STRICT&#039;);
        $view-&gt;headTitle(&#039;My First Zend Framework Application&#039;);

        // Add it to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            &#039;ViewRenderer&#039;
        );
        $viewRenderer-&gt;setView($view);

        // Return it, so that it can be stored by the bootstrap
        return $view;
    }
}
</pre>


        <p class="para">
            This method will be automatically executed when you bootstrap the
            application, and will ensure your view is initialized according to
            your application needs.
        </p>
    </div>

    <div class="section" id="zend.application.quick-start.next-steps"><div class="info"><h1 class="title">Next steps with Zend_Application</h1></div>
        

        <p class="para">
            The above should get you started with <span class="classname">Zend_Application</span>
            and creating your application bootstrap. From here, you should start
            creating resource methods, or, for maximum re-usability, resource
            plugin classes. Continue reading to learn more!
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.application.introduction.html">Introdu&ccedil;&atilde;o</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.application.html">Zend_Application</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.theory-of-operation.html">Theory of Operation</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.application.html">Zend_Application</a></li>
  <li><a href="zend.application.introduction.html">Introdu&ccedil;&atilde;o</a></li>
  <li class="active"><a href="zend.application.quick-start.html">Zend_Application Quick Start</a></li>
  <li><a href="zend.application.theory-of-operation.html">Theory of Operation</a></li>
  <li><a href="zend.application.examples.html">Examples</a></li>
  <li><a href="zend.application.core-functionality.html">Funcionalidade principal</a></li>
  <li><a href="zend.application.available-resources.html">Available Resource Plugins</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>